<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{ color: 'black', background: '#F4F6F5' }"
    >
      <el-table-column prop="id" label="序号" width="180"> </el-table-column>
      <el-table-column prop="name" label="设施" width="180"> </el-table-column>
      <el-table-column prop="description" label="价格"> </el-table-column>
      <el-table-column prop="" label="操作" width="180">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small" style="color: #FB9678;"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-button type="primary" class="buttAu" @click="dialogFormVisiblee()"
      >添加</el-button
    >
    <el-dialog title="设施添加" :visible.sync="dialogFormVisiblea" width="25%">
      <el-form :model="form">
        <el-form-item
          label="设施："
          :label-width="formLabelWidth"
          style="width: 70%"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="价格："
          :label-width="formLabelWidth"
          style="width: 70%"
        >
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisiblea = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisibleae">确 定</el-button>
      </div>
    </el-dialog>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="count"
      :page-size="limit"
      :current-page="page"
      @current-change="change"
    >
    </el-pagination>
  </div>
</template>

<script>
import { Loading } from "element-ui";
export default {
  data() {
    return {
      count: 0,
      page: 1,
      limit: 4,
      dialogFormVisiblea: false,
      tableData: [],
      form: {
        name: "",
        description: "",
      },
      formLabelWidth: "120px",
    };
  },
  methods: {
    change(pagenaw) {
      this.page = pagenaw;
      this.tableData = this.tableData.slice(
        this.page * this.limit - this.limit,
        this.page * this.limit
      );
      this.$axios({
        method: "get",
        url: "/facility/list",
        params: {
          page: this.page,
          limit: this.limit,
        },
      }).then((res) => {
        this.tableData = res.data;
      });
    },

    dialogFormVisiblee() {
      this.dialogFormVisiblea = true;
    },
    dialogFormVisibleae() {
      this.$axios({
        method: "post",
        url: "/facility/insert",
        data: {
          name: this.form.name,
          description: this.form.description,
        },
      }).then((res) => {
        console.log(res.data.msg);
        this.obct();
      });
      this.dialogFormVisiblea = false;
    },
    obct() {
      let loadingInstance = Loading.service({ text: "加载中" });
      this.$axios({
        method: "get",
        url: "/facility/list",
        params: {
          page: this.page,
          limit: this.limit,
        },
      }).then((res) => {
        this.tableData = res.data;
        this.count = res.count;
        console.log(this.count);
        loadingInstance.close();
      });
    },
    handleClick(row) {
      console.log(row.id);
      this.$axios({
        methods: "get",
        url: "/facility/delete",
        params: {
          id: row.id,
        },
      }).then((res) => {
        if (res.code === 0) {
          this.$message.success("删除成功");
          this.obct();
        } else {
          this.$message.error("删除成功");
        }
      });
    },
  },
  created() {
    this.obct();
  },
};
</script>

<style scoped>
.buttAu {
  float: right;
  margin-right: 50px;
}
</style>